package com.jt.www.job.monitor;

import com.jt.www.common.service.MonitorErrorService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.Trigger;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;
import org.springframework.scheduling.support.CronTrigger;
import org.springframework.stereotype.Component;

/**
 * 异常监控
 */
@Component
@EnableScheduling
public class MonitorErrorTask implements SchedulingConfigurer {

    public static final Logger log = LoggerFactory.getLogger(MonitorErrorTask.class);
    @Autowired
    private MonitorErrorService monitorErrorService;

    /**
     * 任务开关
     */
    @Value("${jtpf.monitor.open}")
    private int open;

    /**
     * 任务执行周期
     */
    @Value("${jtpf.monitor.cron}")
    private String cron;

    @Override
    public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
        if (open != 1) {
            return;
        }
        // 如果一个定时任务在执行，那么下一个定时不能执行
        Runnable task0 = () -> monitorErrorService.process();
        Trigger trigger0 = triggerContext -> new CronTrigger(cron).nextExecutionTime(triggerContext);
        taskRegistrar.addTriggerTask(task0, trigger0);
    }

}